import requests
import json

import os
import sys


from html.parser import HTMLParser

def rpc(host, page_no=None):
    uri = host
    if page_no:
        uri = uri + '?Paging=' + str(page_no)

    headers = {}
    params = {}
    data = {}

    rspd = requests.get(uri,params=params,
                      data=json.dumps(data),
                      headers=headers)
                      
    return rspd.text

class BaseHTMLParser(HTMLParser):
    def __init__(self, host):
        super(BaseHTMLParser, self).__init__()
        self._host = host
        self._result = None

    def rpc(self, page_no=None):
        return rpc( self.host, page_no)

    def parser(self, page_no ):
        html_code = self.rpc( page_no )
        self.feed(html_code)
        self.close()
        self._result = self.handle_result()
        return self._result

    @property
    def result(self):
        return self._result
        
    def handle_result(self):
        return None


class MyHTMLParser(BaseHTMLParser):
    HOST = 'http://www.hnggzy.com/hnsggzy/jyxx/002002/002002001/'
    TITLE = '河南省公共资源交易中心'
    #HOST = 'http://www.hnggzy.com/hnsggzy/infodetail/?infoid=032d5eaf-251a-4f4c-8d73-3120cb4b9f3e&categoryNum=002002001'

    def __init__(self):
        super(MyHTMLParser, self).__init__(HOST)
        self.host = HOST
        self.title = TITLE
        
        self.links = {}
        self.tr = 0
        self.td = 0
        self.tag = ''

    def handle_starttag(self, tag, attrs):
        #print("Encountered a start tag:", tag)
        #print("Encountered a start tag:", tag, self.tr, self.td )
        if tag.lower() == "tr":   
            self.tr += 1
            self.links[self.tr] = {}

        elif tag.lower() == "td":   
            self.td += 1 
            self.links[self.tr][self.td] = {}

        elif tag.lower() == "a":   
            if len(attrs):
                for (variable, value) in attrs:   
                    if variable.lower() == "href":   
                        self.links[self.tr][self.td]['href'] = value
                    elif variable.lower() == "title":   
                        self.links[self.tr][self.td]['title'] = value
                        #print(value)

        self.tag = tag.lower()
        
    def handle_data(self, data):
        #print("Encountered some data  :", data)
        if self.tag == 'font':
            #print("Encountered some data:", self.tag, self.tr, self.td, data  )
            tr = self.links.get(self.tr, None)
            if tr != None :
                td = tr.get(self.td, None)
                if td != None:
                    date = td.get('date', None)
                    if date == None:
                        td['date'] = [data]
                    else:
                        date += [data]

    def handle_result(self):
        results = []
        for tr, link in hp.links.items():
            if len(link) < 4:
                continue

            vals = list( link.values() )

            href = vals[1].get('href')
            if not href:
                continue

            title = vals[1].get('title')
            if not title:
                continue

            date = vals[2].get('date')
            if not date:
                continue

            date = date[0]
            results += [ { 'href': href, 'title': title, 'date': date } ]

        def fn(index, res):
            res['id'] = index+1
            return res


        results = [ fn(index, res) for index, res in enumerate( results ) ]
        return results


if __name__ == "__main__":
    page_no = 1

    if len( sys.argv ) > 1:
        page_no = sys.argv[1]

    hp = MyHTMLParser()
    results = hp.parser(page_no)

    print(hp.title, '第', page_no, '页')
    print(hp.host + '?Paging=' + str(page_no) )
    print('')


    for res in results:
        print ( 'no:', res['id'] )
        print ( 'href:', res['href'] )
        print ( 'title:',res['title'] )
        print ( 'date:', res['date'] )
        print ( '')

